﻿$(function () {
    UploadAttFile();

    app.modals.ImportModal = function () {
        var _outboundService = abp.services.app.outbound;
        var _modalManager;
        var _$form = null;
        this.init = function (modalManager) {
            _modalManager = modalManager;
            _$form = _modalManager.getModal().find('form[name=ObjectForm]');
            _$form.validate();
        };
        //保存
        $('#btnSaveImport').click(function () {
            
            if ($("#TempFilePath").val() == "") {
                abp.message.info(app.localize('ImportMessage'));
                return false;
            }
            abp.message.confirm(
                 app.localize('ImportConfirmInfo'),
                 function (isConfirmed) {
                     if (isConfirmed) {
                         //$.blockUI({ message: '<h3>正在导入▪▪▪</h3>' });
                         $.ajax({
                             type: "POST",
                             url: '/Inventory/Outbounds/OutboundImport',
                             data: {
                                 "tempFilePath": $("#TempFilePath").val()
                             },
                             success: function (msg) {
                                 if (msg.result == "") {
                                     //导入成功
                                     abp.notify.info(app.localize('Com_Msg_SuccessfullyImport'));
                                     //关闭窗体
                                     _modalManager.close();
                                     //刷新父窗体   
                                     abp.event.trigger('app.ImportModalSaved'); //调用index.js里面的函数
                                 }
                                 else {
                                     abp.message.info(msg.result);
                                     $(".uploadify-queue-item").remove();
                                     $(".uploadify").css("display", "block");
                                 }
                             }
                         });
                         //abp.notify.success(app.localize('SuccessfullyDeleted'));
                     }
                 }
             );
            UploadAttFile();
        })
    };
})

//上传文件
function UploadAttFile() {
    $('#btnUploadFile').uploadifive({
        'uploadScript': "/Inventory/Outbounds/UploadOutboundFile", 
        'buttonText': '选择文件',
        'width': '100',
        //'height'                : '30',
        'buttonClass': false,
        'dnd': false,
        'fileType': ".xlsx",
        //'uploadLimit':1,
        'multi': true,
        'removeCompleted': false,
        'queueID': false,
        'onError': function (errorType) {
            alert(errorType);
        },
        'onUploadComplete': function (file, data) {
            $("#TempFilePath").val(data);
            //var fileUrl = "<a href='" + data + "'>" + file.name + "</a>";
            //$("#" + file.queueItem.attr("id")).find(".filename").html(fileUrl);
            $("#" + file.queueItem.attr("id")).find(".close").attr("onclick", "javascript:deleteUploadFile('" + file.queueItem.attr("id") + "','" + data + "')");
        }
    });
}
//保存导入
function saveImport() {
    if ($("#TempFilePath").val() == "") {
        abp.message.info(app.localize('ImportMessage'));
        return false;
    }
    abp.message.confirm(
         app.localize('ImportConfirmInfo'),
         function (isConfirmed) {
             if (isConfirmed) {
                 //$.blockUI({ message: '<h3>正在导入▪▪▪</h3>' });
                 $.ajax({
                     type: "POST",
                     url: '/Inventory/Outbounds/InboundImport',
                     data: {
                         "tempFilePath": $("#TempFilePath").val()
                     },
                     success: function (msg) {
                         if (msg.result == "") {
                             //导入成功
                             abp.notify.info(app.localize('SavedSuccessfully'));
                             //关闭窗体
                             _importModalManager.close();
                         }
                         else {
                             abp.message.info(msg.result);
                             $(".uploadify-queue-item").remove();
                             $(".uploadify").css("display", "block");
                         }
                     }
                 });
                 //abp.notify.success(app.localize('SuccessfullyDeleted'));
             }
         }
     );
    UploadAttFile();
}

//删除上传文件
function deleteUploadFile(fileID, filePath) {
    $.ajax({
        type: "POST",
        url: '/Inventory/Outbounds/DeleteUploadTempFile',
        data: "filePath=" + filePath,
        success: function (msg) {
            //msg = eval("(" + msg + ")");
            if (msg.result == "") {
                $("#" + fileID).remove();
                $("#TempFilePath").val("");
            } else {
                abp.message.info(msg.result);
            }
            $(".uploadify").css("display", "block");
        }
    });
}